{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f3d61c75",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7e4bff66",
   "metadata": {},
   "outputs": [],
   "source": [
    "score = np.array(\n",
    "[[80, 89, 86, 67, 79],\n",
    "[78, 97, 89, 67, 81],\n",
    "[90, 94, 78, 67, 74],\n",
    "[91, 91, 90, 67, 69],\n",
    "[76, 87, 75, 67, 86],\n",
    "[70, 79, 84, 67, 84],\n",
    "[94, 92, 93, 67, 64],\n",
    "[86, 85, 83, 67, 80]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "40a1a062",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[80, 89, 86, 67, 79],\n",
       "       [78, 97, 89, 67, 81],\n",
       "       [90, 94, 78, 67, 74],\n",
       "       [91, 91, 90, 67, 69],\n",
       "       [76, 87, 75, 67, 86],\n",
       "       [70, 79, 84, 67, 84],\n",
       "       [94, 92, 93, 67, 64],\n",
       "       [86, 85, 83, 67, 80]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24f3e742",
   "metadata": {},
   "source": [
    "# 效率对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "40480cc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 56.8 ms\n",
      "Wall time: 13 ms\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np\n",
    "a = []\n",
    "for i in range(10000000):\n",
    "    a.append(random.random())\n",
    "# 通过%time魔法方法, 查看当前行的代码运行一次所花费的时间\n",
    "%time sum1=sum(a)\n",
    "\n",
    "b=np.array(a)\n",
    "\n",
    "%time sum2=np.sum(b) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a2262938",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8, 5)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ndarry的属性\n",
    "score.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0fc19670",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "44cc2a93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "40"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f2c49623",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.itemsize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5842638f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f4e1811",
   "metadata": {},
   "source": [
    "ndarray的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "be0ec5e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "749cf07e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "63036379",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "44ed4dba",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_change=np.random.normal(0,1,(8,10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6c17af1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.67821118,  1.24193974,  0.37292217,  0.25455247,  0.12608638,\n",
       "         0.76334994,  0.92947434, -0.46134112, -0.034701  ,  0.18927743],\n",
       "       [-0.4931689 ,  0.69135121,  0.57047272,  0.77924403,  0.88558424,\n",
       "         0.65877054,  0.20493953, -0.22809799,  0.41508378, -0.12085871],\n",
       "       [ 0.61731813,  0.51495486,  0.03450733, -0.89865595, -0.76757188,\n",
       "        -0.80345633,  0.81999092,  0.82545049, -0.50458241,  0.21771489],\n",
       "       [ 0.54207259,  1.37594866,  0.50622495, -0.77052675, -0.04294406,\n",
       "        -0.06204581, -0.8258495 , -1.16670028, -0.27053388, -1.67713971],\n",
       "       [ 0.40738645,  0.59275523,  0.58455212, -0.2856905 , -0.27327842,\n",
       "        -0.60004212, -0.67250664,  0.03672097,  0.01531005,  0.7266215 ],\n",
       "       [ 0.81542552,  1.40562655, -0.43520934, -0.78684306, -0.09799317,\n",
       "        -1.32816958, -0.13857563, -1.18833688, -2.20977298,  0.60592789],\n",
       "       [-0.88542474,  1.13752476, -0.82496153,  0.05322498, -0.13098977,\n",
       "        -1.31849222,  0.39116005,  0.88969622, -2.73445197, -0.59881612],\n",
       "       [ 0.53198177, -0.00697776, -0.03533836,  0.28476219,  1.92590035,\n",
       "        -1.00262698, -0.72870979,  0.57052352,  0.80716926, -0.22346547]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e8ebc485",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_c =stock_change[0:5,0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "281cb8c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.67821118,  1.24193974,  0.37292217,  0.25455247,  0.12608638],\n",
       "       [-0.4931689 ,  0.69135121,  0.57047272,  0.77924403,  0.88558424],\n",
       "       [ 0.61731813,  0.51495486,  0.03450733, -0.89865595, -0.76757188],\n",
       "       [ 0.54207259,  1.37594866,  0.50622495, -0.77052675, -0.04294406],\n",
       "       [ 0.40738645,  0.59275523,  0.58455212, -0.2856905 , -0.27327842]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8a00ef02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False,  True, False, False, False],\n",
       "       [False, False, False, False, False],\n",
       "       [False, False, False, False, False],\n",
       "       [False,  True, False, False, False],\n",
       "       [False, False, False, False, False]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c>1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4c1e42cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_c[stock_c>1]=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7093218a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.67821118,  2.        ,  0.37292217,  0.25455247,  0.12608638],\n",
       "       [-0.4931689 ,  0.69135121,  0.57047272,  0.77924403,  0.88558424],\n",
       "       [ 0.61731813,  0.51495486,  0.03450733, -0.89865595, -0.76757188],\n",
       "       [ 0.54207259,  2.        ,  0.50622495, -0.77052675, -0.04294406],\n",
       "       [ 0.40738645,  0.59275523,  0.58455212, -0.2856905 , -0.27327842]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2c8d8bb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_d=stock_change[0:2,0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ce1599d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.67821118,  2.        ,  0.37292217,  0.25455247,  0.12608638],\n",
       "       [-0.4931689 ,  0.69135121,  0.57047272,  0.77924403,  0.88558424]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e8b58d4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all(stock_d>0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1d9f3005",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.any(stock_d>0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8fb91084",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 1, 1, 1],\n",
       "       [0, 1, 1, 1, 1]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(stock_d>0,1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e90540e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 1, 1, 1],\n",
       "       [1, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(np.logical_and(stock_d>-0.5,stock_d<0.5),1,0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf00022b",
   "metadata": {},
   "source": [
    "# 数组运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d75c4c00",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr =np.array([1,2,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ca4180e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "536b6855",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 6, 8])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "12aea410",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])\n",
    "arr2 = np.array([[1, 2, 3, 4], [3, 4, 5, 6]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ac8ed5d",
   "metadata": {},
   "source": [
    "# arr1+arr2 不可以运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1ab251da",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[80, 86],\n",
    "[82, 80],\n",
    "[85, 78],\n",
    "[90, 90],\n",
    "[86, 82],\n",
    "[82, 90],\n",
    "[78, 80],\n",
    "[92, 94]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1c9d4e33",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([[0.7], [0.3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e0f28858",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[81.8],\n",
       "       [81.4],\n",
       "       [82.9],\n",
       "       [90. ],\n",
       "       [84.8],\n",
       "       [84.4],\n",
       "       [78.6],\n",
       "       [92.6]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e9e6377b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[81.8],\n",
       "       [81.4],\n",
       "       [82.9],\n",
       "       [90. ],\n",
       "       [84.8],\n",
       "       [84.4],\n",
       "       [78.6],\n",
       "       [92.6]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3a4b6886",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[800, 860],\n",
       "       [820, 800],\n",
       "       [850, 780],\n",
       "       [900, 900],\n",
       "       [860, 820],\n",
       "       [820, 900],\n",
       "       [780, 800],\n",
       "       [920, 940]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(a,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "708c1268",
   "metadata": {},
   "source": [
    "# np.matmul(a,10) matmul不支持矩阵和数字的相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec3e33dc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
